package top.haijunit.work.controller.demo;

import cn.hutool.core.text.CharSequenceUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.haijunit.work.core.doamin.Result;

/**
 * 测试请求
 *
 * @author zhanghaijun
 * @since 2024-08-07
 */
@Slf4j
@RestController
@RequestMapping("/test")
@ApiSupport(order = 1)
@Tag(name = "测试请求")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class TestController {

    private final JavaMailSender mailSender;

    @ApiOperationSupport(order = 1)
    @Operation(summary = "测试GET", description = "测试GET")
    @GetMapping("/get")
    public ResponseEntity<String> demoGet(String name) {
        return ResponseEntity.ok(CharSequenceUtil.blankToDefault(name, "Hello World"));
    }

    @ApiOperationSupport(order = 2)
    @Operation(summary = "测试发送邮件", description = "测试发送邮件")
    @GetMapping("/send")
    public Result<Void> send() {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("dev_blog@163.com");
        message.setTo("zhanghaijun_java@163.com");
        message.setSubject("主题：简单邮件");
        message.setText("测试邮件内容");
        mailSender.send(message);
        return Result.success();
    }

    @ApiOperationSupport(order = 3)
    @Operation(summary = "认证异常", description = "测试ResponseEntity返回，携带Response code 400")
    @GetMapping("/error")
    public ResponseEntity<String> errorParma() {
        return ResponseEntity.badRequest().body("参数异常！！！");
    }
}
